home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr47
/
pctuto.zip
/
DISK2.EXE
/
lha
/
CHAP0-1.DOC
< prev
next >
Wrap
Text File
|
1990-08-04
|
42KB
|
897 lines
i
CHAPTER 0.1 - NUMBERS AND ARITHMETIC
You don't habitually use the base two system to balance your
checkbook, so it would be counterproductive to teach you machine
arithmetic on a base two system. What number systems have you had
a lot of experience with? The base 10 system springs to mind. I'm
going to show you what happens on a base 10 system so you will
understand the structure of what happens with computer
arithmetic.
BASE 10 MACHINE
Each place inside the microprocessor that can hold a number is
called a REGISTER. Normally there are a dozen or so of these. Our
base 10 machine has 4 digit registers. They can represent any
number from 0000 to 9999. They are exactly like an industrial
counters or the counters on your tape machines.{1} If you add 27
to a register, the microprocessor counts forward 27; if you
subtract 153 from a register, the microprocessor counts backwards
153. Every time you add 1 to a register, it increments by 1 -
that is 0245, 0246, 0247, 0248. Every time you subtract 1 from a
register, it decrements by 1 - that is 3480, 3479, 3478, 3477.
Let's do some more incrementing. 9997, 9998, 9999, 0000, 0001,
0002. Whoops! That's a problem. When the register reaches 9999
and we add 1, it changes to 0000, not 10,000. How can we tell the
difference between 0000 and 10,000? We can't without a little
help from the CPU.{2} Immediately after an arithmetical
operation, the CPU knows whether you have gone through 10,000
(9999->0000). The CPU has something called a carry flag. It is
internal to the CPU and can have the value 0 or 1. After each
arithmetical operation, the CPU sets the CARRY FLAG to 1 if you
went through the 9999/0000 boundary, and sets the carry flag to 0
if you didn't.{3}
Here are some examples, showing addition, the result, and the
carry flag. The carry flag is normally abbreviated by CF.
number 1 number 2 result CF
0289 4782 5071 0
4398 2964 7382 0
8177 5826 4003 1
____________________
1. Exactly like industrial counters that have several hundred
thousand parts, that is.
2. The CPU (central processing unit) is the chip(s) that does
all the arithmetic. In the case of the PC, it is the 8086.
3. When you set a flag to 0, it is called CLEARING the flag.
______________________
The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
The PC Assembler Tutor ii
______________________
6744 4208 0952 1
Note that you must check the carry flag immediately after the
arithmetical operation. If you wait, the CPU will reset it after
the next arithmetical operation.
Now let's do some decrementing. 0003, 0002, 0001, 0000, 9999,
9998. Golly gosh! Another problem. When we got to 0000, rather
than getting -1, -2, we got 9999, 9998. Apparently 9999 stands
for -1, 9998 stands for -2. Yes, that's the system on this, on
the 8086, and on all computers. (Back to that in a moment.) How
do we tell that the number went through 0 ; i.e. 0000->9999? The
carry flag comes to the rescue again. If the number goes through
the 9999/0000 boundary in either direction, the CPU sets the CF
to 1; if it doesn't, the CPU sets the CF to 0. Here's some
subtraction, with the result and the carry flag.
number 1 number 2 result CF
8473 2752 5721 0
2836 4583 1747 1
0654 9281 8627 1
9281 0654 8627 0
Look at examples 3 and 4. The numbers are reversed. The results
are the same but they have different signs. But that is as it
should be. When you reverse the order in a subtraction, you get
the same absolute value, only a different sign (15 - 7 = 8 but
7 - 15 = -8). Remember, the CF is reliable only immediately after
the operation.
NEGATIVE NUMBERS
The negative numbers go 9999=-1, 9998=-2, 9997=-3, 9996=-4,
9995=-5 etc. A more negative number is denoted by a smaller
number in the register; -5 = 10,000 -5 = 9995; -498 = 10,000 -498
= 9502, and in general, -x = 10,000 -x. Here are some negative
numbers and their representations on our machine.
number machine no number machine no
-27 9973 -4652 5348
-8916 1084 -6155 3845
As you will notice, these numbers look exactly the same as the
unsigned numbers. They ARE exactly the same as the unsigned
numbers. The machine has no way of knowing whether a number in a
register is signed or unsigned. Unlike BASIC or PASCAL which will
complain whenever you try to use a number in an incorrect way,
the machine will let you do it. This is the power and the curse
of machine language. You are in complete control. It is your
responsibility to keep track of whether a number is signed or
unsigned.
Which signed numbers should be positive and which negative? This
has already been decided for you by the computer, but let's think
Chapter 0.1 - Numbers and Arithmetic iii
____________________________________
out what a reasonable solution might be. We could have from 0000
to 8000 positive and from 9999 to 8001 negative, but that would
give us 8001 positive numbers and 1999 negative numbers. That
seems unbalanced. More importantly, if we take -(3279) the
machine will give us 6721, which is a POSITIVE number. We don't
want that. For reasons of symmetry, the positive numbers are
0000-4999 and the negative numbers are 9999-5000.{4} Our most
negative number is -5000 = 10,000 -5000 = 5000.
10'S COMPLEMENT
It's time for a digression. If we are going to be using negative
numbers like -(473), changing from an external number to an
internal number is going to be a bother: i.e. -473 -> 9527. Going
the other way is going to be a pain too: i.e. 9527 -> -473. Well,
it would be a problem except that we have some help.
0000 = 10,000 = 9999 +1
- 473
result 9526 +1 = 9527
Let's work this through carefully. On our machine, 0000 and
10000 (9999+1) are the same thing, so 0 - 473 is the same as
9999+1-473 which is the same as 9999-473+1. But when we have all
9s, this is a cinch. We never have to borrow - all we have to do
is subtract each digit from 9 and then add 1 to the total. We may
have to carry at the end, but that is a lot better than all those
borrows. We'll do a few examples:
(-4276)
0000 = 10,000 = 9999 +1
-4276
result 5723 +1 = 5724
(-3982)
0000 = 10,000 = 9999 +1
-3982
result 6017 +1 = 6018
(-2400)
0000 = 10,000 = 9999 +1
-2400
result 7599 +1 = 7600
(-1989)
0000 = 10,000 = 9999 +1
____________________
4. That way, if we tell the machine that we are working with
signed numbers, all it has to do is look at the left digit. If
the digit is 5-9, we have a negative number, if it is 0-4, we
have a positive number. Note that 0000 is considered to be
positive. This is true on all computers.
The PC Assembler Tutor iv
______________________
-1989
result 8010 +1 = 8011
This is called 10s complement. Subtract each digit from 9, then
add 1 to the total. One thing we should check is whether we get
the same number back if we negate the negative result; i.e. does
-(-1989)) = 1989? From the last example, we see that -1989 =
8011, so:
(-8011)
0000 = 10,000 = 9999 +1
-8011
result 1988 +1 = 1989
It seems to work. In fact, it always works. See the footnote for
the proof.{5} You are going to use this from time to time, so you
might as well practice some. Here are 10 numbers to put into 10s
complement form. The answers are in the footnote. (1) -628, (2)
-4194, (3) -9983, (4) -1288, (5) -4058, (6) -6952, (7) -162, (8)
-9, (9) -2744, (10) -5000.{6}
The computer keeps track of whether a number is positive or
negative. After an arithmetical operation, it sets a flag to tell
whether the result is positive or negative. This flag has no
meaning if you are using unsigned numbers. The computer is
saying, "If the last arithmetical operation was with signed
numbers, then this is the sign of the result." The flag is called
the sign flag (SF). It is 0 if the number is positive and 1 if
the number is negative. Let's decrement again and look at both
the sign flag and carry flag.
NUMBER SIGN CARRY
3 0 0
2 0 0
1 0 0
0 0 0
9999 1 1
____________________
5. Let x be any number. Then:
-x = ( 10,000 - x) = ( 9999 + 1 - x ) ;
-(-x) = ( 10,000 - (-x) ) = ( 9999 + 1 - (-x) )
= ( 9999 + 1 - ( 9999 + 1 - x ) )
= ( 9999 + 1 - 9999 - 1 + x )
= x
6. (1) -628 = 9372 , (2) -4194 = 5806 , (3) -9983 = 0017,
(4) -1288 = 8712 , (5) -4058 = 5942 , (6) -6952 = 3048
(7) -162 = 9838 , (8) -9 = 9991 , (9) -2744 = 7256,
(10) -5000 = 5000. This last one is a little strange. It
changes 5000 into itself. In our system, 5000 is a negative
number and it winds up as a negative number. This happens on all
computers. If you take the maximum negative number and take its
negative, you get the same number back.
Chapter 0.1 - Numbers and Arithmetic v
____________________________________
9998 1 0
9997 1 0
9996 1 0
That worked pretty well. The sign flag changed from 0 to 1 when
we went from 0 to 9999 and the carry flag was set to 1 for that
one operation so we could see that we had gone through the
9999/0000 boundary.
Let's do some more decrementing.
NUMBER SIGN CARRY
5003 1 0
5002 1 0
5001 1 0
5000 1 0
4999 0 0
4998 0 0
4997 0 0
4996 0 0
This one didn't work too well. 5000 is our most negative number
(-5000) and 4999 is our most positive number; when we crossed the
4999/5000 boundary, the sign changed but there was nothing to
tell us that the sign had changed. We need to make another flag.
This one is called the overflow flag. We check the carry flag
(CF) for the 0000/9999 boundary and we check the overflow flag
for the 5000/4999 boundary. The last decrementing example with
the overflow flag:
NUMBER SIGN CARRY OVERFLOW
5003 1 0 0
5002 1 0 0
5001 1 0 0
5000 1 0 0
4999 0 0 1
4998 0 0 0
4997 0 0 0
4996 0 0 0
This time we can find out that we have gone through the boundary.
We'll come back to how the computer sets the overflow flag later,
but let's do some addition and subtraction now.
UNSIGNED ADDITION AND SUBTRACTION
Unsigned addition is done the same way as normally. The computer
adds the two numbers. If the result is over 9999, it sets the
carry flag and drops the left digit (i.e. 14625 -> 4625, CF = 1,
19137 -> 9137 CF = 1, 10000 -> 0000 CF = 1). The largest possible
addition is 9999 + 9999 = 19998. This still has a 1 in the left
digit. If the carry flag is set after an addition, the result
must be between 10000 and 19998.
The PC Assembler Tutor vi
______________________
Since this is unsigned addition, we won't worry about the sign
flag or the overflow flag for the moment. Here are some examples
of unsigned addition.
NUMBER 1 NUMBER 2 RESULT CF
5147 2834 7981 0
6421 8888 5309 1
2910 6544 9454 0
6200 6321 2521 1
Directly after the addition, the computer has complete
information about the number. If the carry flag is set, that
means that there is an extra 10,000, so the result of the second
example is 15309 and the result of the fourth example is 12521.
There is no way to store all that information in 4 digits in
memory so that extra information will be lost if it is not used
immediately.
Subtraction is similar. The machine subtracts, and if the answer
is below 0000, it sets the carry flag, borrows 10000 and adds it
to the result. -3158 -> -3135 + 10000 -> 6842 CF = 1 ; -8197 ->
-8197 + 10000 -> 1803 CF = 1. After a subtraction, if the carry
flag is set, you know the number is 10000 too big. Once again,
the carry flag information must be used immediately or it will be
lost. Here are some examples:
NUMBER 1 NUMBER 2 RESULT CF
3872 2655 1217 0
9826 5967 3859 0
4561 7143 7418 1
2341 4907 7434 1
If the carry flag is set, the computer borrowed 10000, so example
3 is 7418 - 10000 = -2582 and example 4 is 7434 - 10000 = -2566.
MODULAR ARITHMETIC
What the computer is doing is modular arithmetic. Modular
arithmetic is like a clock. If it is 11 o'clock and you go
forward 1 hour it's now 12 o'clock; if it's 11 and you go
backwards 1 hour it's now 10. If it's 11 and you go forward 4
hours it's not 15, it's 3. If it's 11 and you go backward 15
hours it's not -4, it's 8.
The clock is doing mod 12 arithmetic.{7}
(A+B) mod 12
(A-B) mod 12
From the clock's viewpoint, 11 o'clock today, 11 o'clock
yesterday and 11 o'clock, June 8, 1754 are all the same thing. If
____________________
7. To be a perfect analogy 12 o'clock should be 0 o'clock.
Chapter 0.1 - Numbers and Arithmetic vii
____________________________________
you go forward 200 hours (that's 12X16 + 8) you will have the
same result as going forward 8 hours. If you go backwards 200
hours (that's -(12X16 + 8) = -(12X16) -8) you get the same result
as going backwards 8 hours. If you go forward 4 hours from 11
(11+4) mod 12 = 3 you get the same result as going backwards 8
hours (11-8) mod 12 = 3. In fact, these come in pairs. If A + B =
12, then going forward A hours gives the same result as going
backwards B hours. Forwards 9 = backwards 3; forwards 7 =
backwards 5; forwards 11 = backwards 1.
In the mod 12 system, the following things are equivalent:
(+72 + 4) (+72 - 8)
(+60 + 4) (+60 - 8)
(+48 + 4) (+48 - 8)
(+36 + 4) (+36 - 8)
(+24 + 4) (+24 - 8)
(+12 + 4) (+12 - 8)
( 0 + 4) ( 0 - 8)
(-12 + 4) (-12 - 8)
(-24 + 4) (-24 - 8)
(-36 + 4) (-36 - 8)
(-48 + 4) (-48 - 8)
(-60 + 4) (-60 - 8)
They form what is known as an equivalence class mod 12. If you
use any one of them for addition or subtraction, you will get the
same result (mod 12) as with any other one. Here's some
addition:{8}
(+48 + 4) + 7 = (48 + 11) mod 12 = 11
(-48 - 8) + 7 = (48 - 1 ) mod 12 = 11
( 0 - 8) + 7 = ( 0 - 1 ) mod 12 = 11
(-60 + 4) + 7 = (-60 +11) mod 12 = 11
And some subtraction:
(+48 + 4) - 2 = (48 + 2 ) mod 12 = 2
(-48 - 8) - 2 = (48 - 10) mod 12 = 2
( 0 - 8) - 2 = ( 0 - 10) mod 12 = 2
(-60 + 4) - 2 = (-60 + 2) mod 12 = 2
Our pretend computer doesn't cycle every 12 numbers, it cycles
every 10,000 numbers - it is a mod 10,000 machine. On our
machine, the number 6453 has the following equivalence class:
(+30000 + 6453) (+30000 - 3547)
(+20000 + 6453) (+20000 - 3547)
(+10000 + 6453) (+10000 - 3547)
( 0 + 6453) ( 0 - 3547)
(-10000 + 6453) (-10000 - 3547)
(-20000 + 6453) (-20000 - 3547)
(-30000 + 6453) (-30000 - 3547)
____________________
8. (-10) mod 12 = 2 ; (-11) mod 12 = 1
The PC Assembler Tutor viii
______________________
Any one of these will act the same as any other one. Notice that
10000 - 3547 is the subtraction that we did to get the
representation of -3547 on the machine.
-3547 = 9999 + 1
3547
6452 + 1 = 6453
6453 and -3547 act EXACTLY the same on this machine. What this
means is that there is no difference in adding signed or unsigned
numbers on the machine. The result will be correct if interpreted
as an unsigned number; it will also be correct if interpreted as
a signed number.
6821 + 3179 = 10000 so -3179 = 6821 and 3179 = -6821
5429 + 4571 = 10000 so -4571 = 5429 and 4571 = -5429
Since -3179 and 6821 act the same on our machine and since -4571
and 5429 act the same, let's do some addition. Take your time so
you understand why the signed and unsigned numbers are giving the
same results mod 10000:
---------------------------------------------------------
6821 + 497 = 7318
-3179 + 497 = (10000 - 3179) + 497 = 10000 -2682 = -2682
7318 + 2682 = 10000 so -2682 = 7318
----------------------------------------------------------
5429 + 876 = 6305
-4571 + 876 = (10000 - 4571) + 876 = 10000 - 3695 = -3695
6305 + 3695 = 10000 so -3695 = 6305
----------------------------------------------------------
Here's some subtraction:
-----------------------------------------------------------
6821 - 507 = 6314
-3179 - 507 = (10000 - 3179) - 507 = 10000 - 3686 = -3686
6314 + 3686 = 10000 so -3686 = 6314
----------------------------------------------------------
5429 - 178 = 5251
-4571 - 178 = (10000 - 4571) - 178 = 10000 - 4749 = -4749
5251 + 4749 = 10000 so -4749 = 5251
-----------------------------------------------------------
It is the same addition or subtraction. Interpreted one way it is
Chapter 0.1 - Numbers and Arithmetic ix
____________________________________
signed addition or subtraction; interpreted another way it is
unsigned addition or subtraction.
The machine could have one operation for signed addition and
another operation for unsigned addition, but this would be a
waste of computer resources. These operations are exactly the
same. This machine, like all computers, has only one integer
addition operation and one integer subtraction operation. For
each operation, it sets the flags of importance for both signed
and unsigned arithmetic.
For unsigned addition and subtraction, CF, the carry flag tells
whether the 0000/9999 boundary has been crossed.
For signed addition and subtraction, SF, the sign flag tells the
sign of the result and OF, the overflow flag tells whether the
result was too negative or too positive.
SIGN EXTENSION
Although our base 10 machine is set up for 4 digit numbers, it is
possible to use it for numbers of any size by writing the
appropriate software. We'll use 12 digit numbers as an example,
though they could be of any length. The first problem is
converting 4 digit numbers into 12 digit numbers. If the number
is an unsigned number, this is no problem (we'll write the number
in groups of 4 digits to keep it readable):
4816 -> 0000 0000 4816
9842 -> 0000 0000 9842
127 -> 0000 0000 0127
what if it is a signed number? The first thing we need to know
about signed numbers is, what is positive and what is negative?
Once again, for reasons of symmetry, we choose positive to be
0000 0000 0000 to 4999 9999 9999 and negative to be 5000 0000
0000 to 9999 9999 9999.{9} This longer number system cycles from
9999 9999 9999 to 0000 0000 0000. Therefore, for longer numbers,
0000 0000 0000 = 1 0000 0000 0000. They are equivalent.
0000 0000 0000 = 9999 9999 9999 + 1.
If it is a positive signed number, it is still no problem (recall
that in our 4 digit system, a positive number is between 0000 and
4999, a negative signed number is between 5000 and 9999). Here
are some positive signed numbers and their conversions:
1974 -> 0000 0000 1974
1 -> 0000 0000 0001
3909 -> 0000 0000 3909
____________________
9. Once again, the sign will be decided by the left hand
digit. If it is 0-4 it is a positive number; if it is 5-9 it is a
negative number.
The PC Assembler Tutor x
______________________
If it is a negative number, where did its representation come
from in our 4 digit system? -x -> 9999 + 1 -x = 9999 - x + 1.
This time it won't be 9999 + 1 but 9999 9999 9999 + 1. Let's have
some examples.
4 DIGIT SYSTEM 12 DIGIT SYSTEM
-1964
9999 + 1 9999 9999 9999 + 1
-1964 -1964
8035 -> 8036 9999 9999 8035 + 1 -> 9999 9999 8036
-2867
9999 + 1 9999 9999 9999 + 1
-2867 -2867
7132 -> 7133 9999 9999 7132 + 1 -> 9999 9999 7133
-182
9999 + 1 9999 9999 9999 + 1
-182 -182
9817 -> 9818 9999 9999 9817 + 1 -> 9999 9999 9818
As you can see, all you need to do to sign extend a negative
number is to put 9s to the left.
Can't those 9s on the left become 0s when we add that 1 at the
end? No. In order for that to happen, the right four digits must
be 9999. But that can only happen if the number to be negated is
0000:
9999 9999 9999 + 1
-0000
9999 9999 9999 + 1 -> 0000 0000 0000
In all other cases, adding 1 does not carry anything out of the
right four digits.
It is impossible to truncate one of these 12 digit numbers to a 4
digit number without making the results unreliable. Here are two
examples:
(number) 0000 0168 7451 -> 7451 (now a negative number)
(actual value) +168 7451 -2549
(number) 9999 9643 2170 -> 2170 (now a positive number)
(actual value) -356 7830 +2170
We now have 12 digit numbers. Is it possible to add them and
subtract them? Yes but only 4 digits at a time. When you add with
pencil and paper you carry left from each digit. The computer can
carry left from each group of 4 digits. We'll do the following
addition:
0138 6715 6037
+ 2514 2759 7784
Chapter 0.1 - Numbers and Arithmetic xi
____________________________________
Do this with pencil and paper and write down all the carries. The
computer is going to do this in 3 parts:
1) 6037 + 7784
2) 6715 + 2759 + carry (if any)
3) 0138 + 2514 + carry (if any)
The first addition is our regular addition. It will set the carry
flag if the 0000/9999 boundary was crossed (i.e. the result was
larger than 9999). In our case CF = 1 since the result is 13821.
The register holds 3821. We store 3821. Next, we need to add
three things: 6715 + 2759 + CF (=1). There is an instruction like
this on all computers. It adds two numbers plus the value of the
carry flag. Our first addition was ADD (add two numbers). This
time the machine instruction is ADC (add two numbers and the
carry). The result of our second addition is 9475. The register
holds 9475 and CF = 0. We store 9475. Finally, we need to add
three more things: 0138 + 2514 + CF (=0). Once again we use ADC.
The result is 2652, CF = 0. We store the 2652. That is the whole
result:
2652 9475 3821
If CF = 1 at this point, the number has crossed the
9999,9999,9999/0000,0000,0000 boundary. This will work for signed
numbers also. The only difference is that at the very end we
don't check CF, we check OF to see if the
4999,9999,9999/5000,0000,0000 boundary has been crossed.
Just to give you one more example we'll do a subtraction using
the same numbers:
0138 6715 6037
2514 2759 7784
Notice that in order for you to do this with pencil and paper
you'll have to put the larger number on top before you subtract.
With the machine this is unnecessary. Go ahead and do the
subtraction with pencil and paper.
The machine can do this 4 digits at a time, so this is a three
step process:
1) 6037 - 7784
2) 6715 - 2759 - borrow (if any)
3) 0138 - 2514 - borrow (if any)
The first one is a regular subtraction and since the bottom
number is larger, the result is 8253, CF = 1. (Perhaps you are
puzzled because that's not the result that you got. Don't worry,
it all comes out in the wash). Step two subtracts but also
subtracts any borrow (We had a borrow because CF = 1). There is a
special instruction called SBB (subtract with borrow) that does
just that. 6715 - 2759 - 1 = 3955, CF = 0. We store the 3955 and
go on to the third part. This also is SBB, but since we had no
The PC Assembler Tutor xii
______________________
borrow, we have 0138 - 2514 - 0 = 7624, CF = 1. We store 7624.
This is the end result, and since CF = 1, we have crossed the
9999,9999,9999/0000,0000,0000 boundary. This is going to be the
representation of a negative number mod 1,0000,0000,0000. With
pencil and paper, your result was:
-2375 6044 1747
The machine result was:
7624 3955 8253
But CF was 1 at the end, so this represents a negative number.
What number does it represent? Let's take its negative to get a
positive number with the same absolute value:
9999 9999 9999 + 1
7624 3955 8253
2375 6044 1746 + 1 = 2375 6044 1747
This is the same thing you got with pencil and paper. The reason
it looked wierd is that a negative number is always stored as its
modular equivalent. If you want to read a negative number, you
need to take its negative to get a positive number with the same
absolute value.
If we had been working with signed numbers, we wouldn't have
checked CF at the very end, we would have checked OF to see if
the 4999,9999,9999/5000,0000,0000 boundary had been crossed. If
OF = 1 at the end, then the result was either too negative or too
positive.
OVERFLOW
How does the machine decide that overflow has occured? First,
what exactly is overflow and when is it possible for overflow to
occur?
Overflow is when the result of a signed addition or subtraction
is either larger than the largest positive number or more
negative than the most negative number. In the case of the 4
digit machine, larger than +4999 or more negative than -5000.
If one number is negative and the other is positive, it is not
possible for overflow to occur. Take +32 and -4791 as examples.
If we start with the positive number (+32) and add the negative
number (-4791), the result can't possibly be too positive.
Similarly, if we start with the negative number (-4791) and add
the positive number (+32), the result can't be too negative.
Therefore, the result can be neither too positive nor too
negative. Make sure you understand this before going on.
What if both are positive? Then overflow is possible. Here are
some examples:
Chapter 0.1 - Numbers and Arithmetic xiii
____________________________________
(+3500) + (+4500) = 8000 = -2000
(+2872) + (+2872) = 5744 = -4256
(+1799) + (+4157) = 5956 = -4044
In each case, two positive numbers give a negative result. How
about two negative numbers?
(7154) + (6000) = 3154 = +3154
(actual value) -2946 -4000
(5387) + (5826) = 1213 = +1213
(actual value) -4613 -4174
(8053) + (6191) = 4244 = +4244
(actual value) -1947 -3809
The numbers underneath are the negative numbers that the numbers
above them represent. In these cases, adding two negative numbers
gives a positive result.
This is what the machine checks for. Before the addition, it
checks the signs of the numbers. If the signs are the same, then
the result must also be the same sign or overflow has
occurred.{10} Thus + and + must have a + result; - and - must
have a - result. If not, OF (the overflow flag) is set (OF = 1).
Otherwise OF is cleared (OF = 0).
MULTIPLICATION
Unsigned multiplication is easy. The machine simply multiplies
the two numbers. Since the result can be up to 8 digits (the
maximum result is 9999 X 9999 = 9998 0001) the machine uses two
registers to hold the result. We'll call them R1 and R2.
5436 X 174 R1 0094
R2 5864
2641 X 2003 R1 0528
R2 9923
You need to know which register holds which half of the result,
but besides that, everything is straightforward. On this machine
R1 holds the left four digits and R2 holds the right four digits.
Notice that our machine has changed the modular base from N to
N*N (from 1 0000 to 1 0000 0000). What this means is that two
things which are modularly equivalent under addition and
subtraction are not necessarily equivalent under multiplication
and division. 6281 and -3719 will not work the same.
____________________
10. The machine checks something considerably more obscure
because it is easier to implement in semiconductor logic, but
what it is actually doing is checking to see if the two numbers
being added have the same sign. If they do, the result must be
the same sign or overflow has occurred.
The PC Assembler Tutor xiv
______________________
The machine can't do signed multiplication. What it actually does
is convert the numbers to positive numbers (if necessary),
perform unsigned multiplication, and then do sign adjustment of
the results (if necessary). It uses 2 registers for the result.
SIGNED MULTIPLICATION REGS RESULT
(number) (5372) X (3195) R1 8521 = -1478 6460
(actual value) -4628 X +3195 R2 3540
(number) (9164) X (8746) R1 0104 = +104 8344
(actual value) -836 X -1254 R2 8344
(number) (9927) X (0013) R1 9999 = -949
(actual value) -73 X +13 R2 9051
Looking at the last example, if we performed unsigned
multiplication on those two numbers, we would have
9927 X 0013 = 0012 9051, a completely different answer from the
one we got. Therefore, whenever you do multiplication, you have
to tell the machine whether you want unsigned or signed
multiplication.
DIVISION
Unsigned division is easy too. The machine divides one number by
the other, puts the quotient in one register and the remainder in
another. Once again, the only problem is remembering which
register has the quotient and which register has the remainder.
For us, the quotient is R1 and the remainder is R2.
6190 / 372 R1 0016 16 remainder 238
R2 0238
9845 / 11 R1 0895 895 remainder 0
R2 0000
As with multiplication, signed division is handled by the machine
changing all numbers to positive numbers, performing unsigned
division, then putting back the appropriate signs.
SIGNED DIVISION REGS RESULT
(number) (7192) / (9164) R1 0003 +3 rem. -300
(actual value)-2808 / -836 R2 9700
(number) (3753) / (9115) R1 9996 -4 rem. +213
(actual value)+3753 / -885 R2 0213
Looking at the last example, 3753 / 9115, if that were unsigned
multiplication the answer would be 0 remainder 3753, a completely
different answer from the signed division. Every time you do a
division, you have to state whether you want unsigned or signed
division.